import WXAD, { RewardADReturn } from "../../../StartAb/WXAd";
import WXRealLogUtil from "../../../StartAb/WXRealLogUtil";
import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import StatusProperties, { RewardType } from "../game/StatusProperties";
import NativeCaller from "../NativeCaller";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import CoinSystem from "../system/CoinSystem";
import ToastHelper from "./ToastHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelEndDropBox extends cc.Component {
    @property(cc.Node)
    boxNode: cc.Node = null;

    @property(cc.SpriteFrame)
    openBoxFrame: cc.SpriteFrame = null;

    @property(cc.Label)
    coinNumLabel: cc.Label = null;

    @property(cc.Node)
    boxFlyCoinNode: cc.Node = null;

    @property({ type: [cc.Node] })
    hideNodes: cc.Node[] = [];

    @property(cc.Node)
    pointerNode: cc.Node = null;

    @property(cc.Button)
    adGetButton: cc.Button = null;
    @property(cc.Button)
    noAdGetButton: cc.Button = null;

    static REWARD_COIN = 30;

    //TODO 倍数计算
    //TODO 加载节点
    reward_times = 2;
    onLoad() {
        this.hideNodes.forEach((n: cc.Node) => {
            n.active = false;
        })
    }

    start() {
        this.coinNumLabel.string = "+" + LevelEndDropBox.REWARD_COIN;
        cc.tween(this.boxNode)
            .to(0.75, { y: 150 }, { easing: "cubicIn" })
            .to(0.20, { scaleX: 1.25, scaleY: 0.9 })
            .to(0.30, { scaleX: 1.0, scaleY: 0.95 })
            .to(0.20, { scaleX: 1.25, scaleY: 0.9 })
            .to(0.30, { scaleX: 1.0, scaleY: 0.95 })
            .to(0.20, { scaleX: 1.25, scaleY: 0.9 })
            .to(0.1, { scaleX: 1, scaleY: 1 })
            .call(() => {
                this.boxNode.getComponent(cc.Sprite).spriteFrame = this.openBoxFrame;
                this.showCoinGetAni();
            })
            .start()

        EventCenter.on(EventNames.RewardedSuccess, () => {
            if (StatusProperties.rewardType == RewardType.ReqLevelPassBox) {
                this.getCoin(LevelEndDropBox.REWARD_COIN * this.reward_times)
            }
        }, this)

        EventCenter.on(EventNames.RewardShowFailed, () => {
            this.adGetButton.interactable = true;
            this.noAdGetButton.interactable = true;
        }, this)
    }

    showCoinGetAni() {
        cc.tween(this.boxFlyCoinNode)
            .parallel(cc.tween(this.boxFlyCoinNode).to(0.5, { scale: 1.0 }), cc.tween(this.boxFlyCoinNode).by(0.5, { y: 150 }))
            .call(() => {
                this.hideNodes.forEach((n: cc.Node) => {
                    n.active = true;
                })
                this.pointerAni();
            })
            .start();
    }

    noAdGet(e: cc.Event.EventTouch) {
        this.adGetButton.interactable = false;
        this.noAdGetButton.interactable = false;
        this.getCoin(LevelEndDropBox.REWARD_COIN)
        ServiceLogic.action(ActionType.ActionPassRewardFree, {})
    }

    adGet(e: cc.Event.EventTouch) {
        this.adGetButton.interactable = false;
        this.noAdGetButton.interactable = false;
        cc.Tween.stopAllByTarget(this.pointerNode);
        this.reward_times = this.calTimes();
        cc.log("level end times:", this.reward_times)
        StatusProperties.rewardType = RewardType.ReqLevelPassBox
        ServiceLogic.action(ActionType.ActionPassRewardAd, {})

        WXAD.instance.showReward((code: number) => {
            // console.log("视频广告回调结果:", code)
            switch (code) {
                case RewardADReturn.PlayRewarded:
                    //看了视频广告，将插屏显示初始化
                    ToastHelper.show("提示成功.")
                    WXRealLogUtil.info('关卡结束广告领取成功')
                    this.getCoin(LevelEndDropBox.REWARD_COIN * this.reward_times)
                    break
                case RewardADReturn.NoAd:
                    ToastHelper.show("广告未准备好,请稍后重试.")
                    WXRealLogUtil.info('关卡结束广告未准备好')

                    this.adGetButton.interactable = true;
                    this.noAdGetButton.interactable = true;
                    break
                case RewardADReturn.PlayNotEnd:
                    ToastHelper.show("要观看完广告才能获得金币翻倍哟.")
                    WXRealLogUtil.info('关卡结束广告未看完')

                    this.adGetButton.interactable = true;
                    this.noAdGetButton.interactable = true;
                    break
            }

        })
    }

    pointerAni() {
        this.pointerNode.parent.active = true
        this.adGetButton.node.active = true;
        this.noAdGetButton.node.active = true;
        cc.tween(this.pointerNode)
            .repeat(1000000,
                cc.tween(this.pointerNode)
                    .to(0.5, { x: 315 }, { easing: "quadOut" })
                    .to(0.5, { x: 0 }, { easing: "quadIn" })
                    .to(0.5, { x: -315 }, { easing: "quadOut" })
                    .to(0.5, { x: 0 }, { easing: "quadIn" })
            )
            .start();
    }

    getCoin(coin: number) {
        this.boxFlyCoinNode.active = false
        EventCenter.emit(EventNames.FlyGetCoinEvent, coin, this.node.convertToWorldSpaceAR(cc.v2(0, 150)));
        CoinSystem.addCoin(coin);
        ServiceLogic.action(ActionType.ActionCoinChanged, { "coinhave": CoinSystem.coinInMomory, "beiz": coin })
        this.scheduleOnce(() => {
            this.node.destroy();
        }, 1.0)
    }

    calTimes() {
        let absX = Math.abs(this.pointerNode.x);
        if (absX <= 68)
            return 5;
        if (absX <= 169)
            return 4;
        if (absX <= 254)
            return 3;
        return 2;
    }
}
